import org.apache.commons.lang3.RandomStringUtils;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * @description:
 * @author: WGY
 * @create: 2018-06-05 16:46
 **/
public class BatchInsert5 {
    public static void main(String[] args) {
        BatchInsert5 ti = new BatchInsert5();
        System.out.println("开始执行……");
        ti.multiThreadImport(1);
//        ti.multiThreadImport(Runtime.getRuntime().availableProcessors());
        System.out.println("笔记本CPU数:" + Runtime.getRuntime().availableProcessors());
    }

    private String url = "jdbc:mysql://106.75.216.203:1006/test_db";
    private String user = "ebs";
    private String password = "ebs123@Ebs";

    public Connection getConnect() {
        Connection con = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    public void multiThreadImport(final int ThreadNum) {
        final CountDownLatch cdl = new CountDownLatch(ThreadNum);
        long starttime = System.currentTimeMillis();
        int x = 30106263;
        for (int k = 1; k <= ThreadNum; k++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    Connection con = getConnect();
                    try {
                        Statement st = con.createStatement();
                        for (int i = 1; i <= 100000 / ThreadNum; i++) {
                            String username = RandomStringUtils.randomAlphanumeric(4);
                            String pwd = RandomStringUtils.randomAlphanumeric(10);
                            String salt = RandomStringUtils.randomAlphanumeric(10);
                            String mobile = RandomStringUtils.randomNumeric(11);
                            int id = x + 100000 + i;
                            st.addBatch("insert into sys_user (id,username, pwd, salt, nickname,status,mobile,email) " +
                                    " values(" + id + ",'" + username + "','" + pwd + "','" + salt + "','" + getRandomJianHan(4)
                                    + "',1,'" + mobile + "','" + username + "@7sh.cn')");
                            if (i % 50 == 0) {
                                st.executeBatch();
                            }
                            System.out.println("==>" + i);
                        }
                        cdl.countDown();
                    } catch (Exception e) {
                    } finally {
                        try {
                            con.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
        try {
            cdl.await();
            long spendtime = System.currentTimeMillis() - starttime;
            System.out.println(ThreadNum + "个线程花费时间:" + spendtime / 1000 + "秒");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

    /**
     * 获取指定长度随机简体中文
     *
     * @param len int
     * @return String
     */
    public static String getRandomJianHan(int len) {
        String ret = "";
        for (int i = 0; i < len; i++) {
            String str = null;
            int hightPos, lowPos; // 定义高低位
            Random random = new Random();
            //获取高位值
            hightPos = (176 + Math.abs(random.nextInt(71)));
            //获取低位值
            lowPos = (161 + Math.abs(random.nextInt(94)));
            byte[] b = new byte[2];
            b[0] = (new Integer(hightPos).byteValue());
            b[1] = (new Integer(lowPos).byteValue());
            try {
                //转成中文
                str = new String(b, "GBK");
            } catch (UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
            ret += str;
        }
        return ret;
    }
}
